package com.example.constellation.partnerfrag;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.*;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.constellation.R;
import com.example.constellation.bean.StarInfoBean;
import com.example.constellation.util.AssetsUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * A simple {@link Fragment} subclass.
 */
public class PartnerFragment extends Fragment{
    ImageView manIv,womanIv;
    Spinner manSp,womanSp;//下拉框菜单栏
    Button prizeBtn,analysisBtn;
    private List<StarInfoBean.StarinfoBean> infoList;
    List<String> namelist;//放星座名
    private Map<String, Bitmap> logoImgMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_partner, container, false);
        init(view);//初始化
        //获取activity传来的数据bundle
        Bundle bundle=getArguments();
        StarInfoBean starbean=(StarInfoBean) bundle.getSerializable("info");
        infoList=starbean.getStarinfo();
        namelist=new ArrayList<>();
        //获取适配器需要的数据源
        for(int i=0;i<infoList.size();i++){
            String name=infoList.get(i).getName();//星座名称
            namelist.add(name);
        }
        //new 适配器
        ArrayAdapter adapter=new ArrayAdapter<String>(getContext(),R.layout.item_partner_sp,R.id.item_partner_tv,namelist);
        //设置适配器
        manSp.setAdapter(adapter);
        womanSp.setAdapter(adapter);
        //获取第一个图片资源显示在imageview上
        String logoname=infoList.get(0).getLogoname();
        logoImgMap = AssetsUtil.getContentlogoImgMap();
        Bitmap bitmap=logoImgMap.get(logoname);
        manIv.setImageBitmap(bitmap);
        womanIv.setImageBitmap(bitmap);
        return view;
    }

    private void init(View view){
        manIv=view.findViewById(R.id.parnterfrag_iv_man);
        womanIv=view.findViewById(R.id.parnterfrag_iv_woman);
        manSp=view.findViewById(R.id.parnterfrag_sp_man);
        womanSp=view.findViewById(R.id.parnterfrag_sp_woman);
        prizeBtn=view.findViewById(R.id.parnterfrag_btn_prize);
        analysisBtn=view.findViewById(R.id.parnterfrag_btn_analysis);
        //按钮监听器
        prizeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        analysisBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    //获取spinner选中的下拉框内的位置下标
                int manSelPos=manSp.getSelectedItemPosition();
                int womanSelPos=womanSp.getSelectedItemPosition();
                //跳转    星座配对详情页
                Intent intent=new Intent(getContext(),PartnerAnalyslsActivity.class);
                //从集合中取出
                intent.putExtra("man_name",infoList.get(manSelPos).getName());
                intent.putExtra("man_logoname",infoList.get(manSelPos).getLogoname());
                intent.putExtra("woman_name",infoList.get(womanSelPos).getName());
                intent.putExtra("woman_logoname",infoList.get(womanSelPos).getLogoname());
                startActivity(intent);
            }
        });
        manSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String logoname=infoList.get(i).getLogoname();
                Bitmap bitmap=logoImgMap.get(logoname);
                manIv.setImageBitmap(bitmap);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        womanSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String logoname=infoList.get(i).getLogoname();
                Bitmap bitmap=logoImgMap.get(logoname);
                womanIv.setImageBitmap(bitmap);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }
}
